import { createAlova } from 'alova';
import GlobalFetch from 'alova/GlobalFetch';
import VueHook from 'alova/vue';

export enum ResponseCode {
  SUCCESS = '000000',
  FAIL = '000001',
}

export interface CResponse extends Response {
  code: ResponseCode
  message: string
  timestamp: string
}

export const HEADERS = {
  USER_NAME: '123456',
  USER_ID: '123456'
}

export const request = createAlova({
  // baseURL: import.meta.env.PROD ?  : '/api',
  baseURL: import.meta.env.PROD ? import.meta.env.VITE_BASE_URL : '/api',
  statesHook: VueHook,
  requestAdapter: GlobalFetch(),
  beforeRequest: (method) => {
    console.log('beforeRequest', method);
    for (const key in HEADERS) {
      method.config.headers[key] = HEADERS[key]
    }

  },
  responded: {
    onSuccess: (response, method) => {
      // console.log('onSuccess', response, method);

      return response
    },
    onError: (err, method) => {
      // console.log('onError', err, method);
    },
    onComplete: (method) => {
      // console.log('onComplete', method);
    }
  }
});
